package com.he.hsdyc.controller;

import com.he.hsdyc.util.BigDecimalUtil;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("test")
@Api(value="TestController",description="只是测试",tags={"只是测试"})
public class TestController {

//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Autowired
//    private TestService testService;


    public void test () {

        Object o = new Object();
        o = null;
        Assert.notNull(o,"参数不允许为空");

        //java语法糖---------数值字面量，编译阶段会自动去掉下划线
        Long l = 100_000_000L;

        //java语法糖---------方法变长参数
        print("1","2","3","4");

        //java语法糖---------switch 支持 String 与枚举
        String s = "test";
        switch (s) {
            case "hello":
                System.out.println("hello");
                break;
            case "test":
                System.out.println("test");
                break;
            default:
                break;
        }
    }

    public static void print(String... strs)
    {
        for (int i = 0; i < strs.length; i++)
        {
            System.out.println(strs[i]);
        }
    }

    @ApiOperation("时间格式测试")
    @RequestMapping(value = "testDate",method = RequestMethod.GET)
    public String testDate(@RequestParam String month) {
        //此种方式月份会自动加1，不推荐
        Date date1 = new Date(2017-1900,12,1,12,00,00);//Sun Jan 18 12:00:36 CST 1970
        Date date2 = new Date(2017-1900,11,18,12,00,00);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sdate1 = sdf.format(date1);//2017-02-01 12:00:00
        String sdate2 = sdf.format(date2);//2017-12-18 12:00:00
        System.out.println("=================sdate1"+sdate1);
        System.out.println("=================sdate1"+sdate2);

        Date  date3 = new Date(1483236000);//2017/01/01 10:00:00    Sun Jan 18 12:00:36 CST 1970
        String sdate3 = sdf.format(date3);
        System.out.println("===================new Date(long)UNIX转北京时间："+sdate3);//1970-01-18 12:00:36,转的是秒而非毫秒，所以转换错误



        //自定义时间，推荐写法
        Date date4 = new Date(1496282400000L);//自定义时间，由于new Date(String s)已经废弃，所以用如下方法，转为UNIX时间戳，后面加L,
        //此时返回的是date默认格式，需要格式转换成我们需要的格式，即由：Sun Jan 18 12:00:36 CST 1970   转为：2017/01/01 10:00:00
        String sdate4 = sdf.format(date4);
        System.out.println("=============="+sdate4);//2017-06-01 10:00:00




      /*  String month_regex = "^(0?[1-9]|1[0-2])$";
        if(month.matches(month_regex))
            System.out.println("==================匹配1-12:success");
        else System.out.println("==================匹配1-12:failed");*/

        //测试正则，是否为正整数
        String regex = "^[1-9]\\d*$";
        if(month.matches(regex))
            System.out.println("==================匹配正整数:success");
        else System.out.println("==================匹配正整数:failed");


        //容易犯错的地方，必须用equals
        Integer a = 2000;
        Integer b= 2000;
        System.out.println("======================2000 == 2000的结果是："+(a == b));
        System.out.println("======================2000.equals(2000)的结果是："+(a.equals(b)));



        //当前时间到设定时间剩余的秒数，倒计时
        Date publishDate = new Date(1515114900000L);//2018/1/5 09:15:00
        Date currentDate = new Date();
        long exprieTime = (publishDate.getTime() - currentDate.getTime()) / 1000;
        System.out.println("====================publishDate:"+publishDate);
        System.out.println("====================currentDate:"+currentDate);
        System.out.println("====================exprieTime:"+exprieTime);


        System.out.println("========================Date"+publishDate);//Dec 26 14:50:00 CST 2017
        System.out.println("========================Date.getTime()"+publishDate.getTime());//1514271000000
        System.out.println("========================Date.format"+sdf.format(publishDate));//2017-12-26 14:50:00


        //运营时长
        Date establishDate =  new Date(1415667600000L);//2014/11/11 9:0:0
        Date curDate = new Date();
        List<String> operateTime = BigDecimalUtil.getDatePoor(establishDate, curDate);
        System.out.println("=========================运营时长:"+operateTime);


        //金额划分
        Double d = 150005555.66;
        List<String> typeConversion = BigDecimalUtil.typeConversion(d);
        System.out.println("=========================累计投资金额"+typeConversion);

        return  "success";
    }




//
//    //本服务中调用可以使用RedisTemplate，但是失效监听不起作用，因为ymlredis使用的数据库和redisStarter中配置的数据库不一样
//    //"测试redis-starters
//    @ApiOperation("测试redisStarter")
//    @RequestMapping(value = "redis",method = RequestMethod.GET)
//    public void testReids(@RequestParam String mobile) {
//        String testKey = "TEST:REDIS:"+mobile;
//        redisTemplate.opsForValue().set(testKey,"success");
//        String template = (String) redisTemplate.opsForValue().get(testKey);
//        System.out.println("=======================redis中保存的键：TEST:REDIS:18888888888--》对应的值是："+template);
//
//        //失效监听
//        String key = BidPulishListener.constructPublishBid(mobile);
//        redisTemplate.opsForValue().set(key,"这是一个定时任务");
//        redisTemplate.expire(key,20, TimeUnit.SECONDS);
//    }
//
//
//    @ApiOperation("用户列表")
//    @RequestMapping(value = "list",method = RequestMethod.GET )
//    public String register() {
//        User user = testService.register();
//        return user.getMobile();
//    }
}
